<template>
	<view>
    <!--售货机购买-->
    <vendingMachine ref="vendingMachine" :deviceNo="deviceVo.deviceNo"
                    v-if="deviceVo.status == 1 && (deviceVo.deviceTypeId == deviceTypeEnum.VENDING_MACHINE ||
			 deviceVo.deviceTypeId == deviceTypeEnum.OPEN_DOOR_VENDING_MACHINE|| deviceVo.deviceTypeId == deviceTypeEnum.BLEND_VENDING_MACHINE)">
    </vendingMachine>
		<!-- #ifdef H5 -->
		<userAgreement :inDeviceNo="deviceNo" :inHierarchy="hierarchy" :inFreightLane="freightLane"></userAgreement>
		<!-- #endif -->
	</view>
</template>

<script>
	import vendingMachine from '@/pages-pay/vending-machine/vending-machine'
  import userAgreement from "@/components/user-agreement/user-agreement";
	export default {
		components: {
      vendingMachine,
      userAgreement
		},
		data() {
			return {
				deviceNo: '',
				deviceVo: {
					status: '',
					deviceNo: '',
					// 层
					hierarchy: '',
					// 列
					freightLane: '',
					deviceTypeId: '',
					merchantStoreId: '',
				},
				deviceTypeEnum: this.$deviceTypeEnum,
				coinNum: 0,
				// 层
				hierarchy: '',
				// 列
				freightLane: '',
			}
		},
		onReady() {
			// #ifdef MP-ALIPAY
			this.getOneDeviceByDeviceNo();
			//#endif
		},
    onShow(){
      if (!uni.$u.test.isEmpty(this.deviceVo) && this.deviceVo.isBluetooth && this.deviceVo.deviceTypeId == this.deviceTypeEnum.OPEN_DOOR_VENDING_MACHINE ){
          this.$bluetooth.reconnect(this.deviceNo);
      }
    },
		onLoad(options) {
			// #ifdef  MP-WEIXIN
			if (options.q) {
				let scan_url = decodeURIComponent(options.q);
				// console.log('scan_url:' + scan_url);
				let index = scan_url.indexOf("deviceNo/");
				let paramArray = [];
				if (index > 0) {
					// console.log('deviceNo/');
					paramArray = scan_url.split("deviceNo/");
					this.deviceNo = paramArray[1];
				} else {
					// console.log('/c');
					const urlParams = this.$helpers.getUrlParams(scan_url);
					this.deviceNo = urlParams.d;
					this.hierarchyAndFreightLaneCheck(urlParams);
				}
			}
      this.$bluetooth.closeBluetoothAdapter();
			// #endif

			// #ifdef  MP-ALIPAY
			let oo = my.getLaunchOptionsSync();
			// console.log("oo:", oo);
			if (oo.query && oo.query.qrCode) {
				let scan_url = oo.query.qrCode;
				// let scan_url = 'http://image.iotbox.cloud/client/api/deviceNo/600002';
				let index = scan_url.indexOf("deviceNo/");
				let paramArray = [];
				if (index > 0) {
					// console.log('deviceNo/');
					paramArray = scan_url.split("deviceNo/");
					this.deviceNo = paramArray[1];
				} else {
					// console.log('/c');
					const urlParams = this.$helpers.getUrlParams(scan_url);
					this.deviceNo = urlParams.d;
					this.hierarchyAndFreightLaneCheck(urlParams);
				}
			}
			// #endif
			if (options.deviceNo) {
				this.deviceNo = options.deviceNo;
			}
			if (options.h) {
				this.hierarchy = options.h;
			}
			if (options.f) {
				this.freightLane = options.f;
			}
			// #ifdef H5
			if (options.token) {
				this.$login.getH5Login(options.token);
			}
			// #endif
			// #ifdef MP-WEIXIN || H5
			this.getOneDeviceByDeviceNo();
			//#endif
		},
    methods: {
			changeDeviceNo(deviceNo) {
				this.deviceNo = deviceNo
				this.getOneDeviceByDeviceNo();
			},
			hierarchyAndFreightLaneCheck(urlParams){
				if (urlParams.h) {
					this.hierarchy = urlParams.h;
				}
				if (urlParams.f) {
					this.freightLane = urlParams.f;
				}
			},
			getOneDeviceByDeviceNo() {
				//console.log("设备号",this.deviceNo)
				if (this.$u.test.isEmpty(this.deviceNo)) {
					getApp().err("设备号为空");
					return
				}

				uni.showLoading({
					title: '加载中...',
				});

				let param = {
					deviceNo: this.deviceNo,
					// 层
					hierarchy: this.hierarchy,
					// 列
					freightLane: this.freightLane,
				}
				this.$api('device.getOneDeviceByDeviceNo', param).then(res => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
					if (res.status == 0) {
						this.deviceVo = res.data
						this.deviceVo.hierarchy = this.hierarchy
						this.deviceVo.freightLane = this.freightLane
						if (this.deviceVo.networkIntensity == 0 && !this.deviceVo.isBluetooth) {
							// 设备状态异常
							this.$Router.replaceAll({
								name: 'device-exception',
								params: {
									deviceNo: this.deviceNo
								}
							});
							return
						}

						if (this.$u.test.isEmpty(this.deviceVo.merchantStoreId) && this.deviceVo.status == 3) {
							if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.VENDING_MACHINE ||
								this.deviceVo.deviceTypeId == this.deviceTypeEnum.SCREEN_VENDING_MACHINE ||
								this.deviceVo.deviceTypeId == this.deviceTypeEnum.OPEN_DOOR_VENDING_MACHINE ||
								this.deviceVo.deviceTypeId == this.deviceTypeEnum.BLEND_VENDING_MACHINE) {
								this.$Router.replaceAll({
									name: 'channel-management',
									params: {
										deviceNo: this.deviceNo,
                    isBluetooth: this.deviceVo.isBluetooth
									}
								});
								return;
							} else {
								// 未激活
								this.$Router.replaceAll({
									name: 'device-debug',
									params: {
										deviceNo: this.deviceNo,
										h:this.hierarchy,
									}
								});
								return
							}

						}

						if (this.deviceVo.status == 4) {
							// 已解绑
							this.$Router.replaceAll({
								name: 'device-not-binding',
								params: {
									deviceNo: this.deviceNo
								}
							});
							return
						}
						if (!this.$u.test.isEmpty(this.deviceVo.merchantStoreId)) {
							getApp().globalData.currMerchantStore.id = this.deviceVo.merchantStoreId;
						}
						if (!this.$u.test.isEmpty(this.deviceVo.storeName)) {
							getApp().globalData.currMerchantStore.storeName = this.deviceVo.storeName;
						}

						this.initComponentData();
					} else {
						this.$helpers.toast(res.msg);
					}
				}).catch(err => {
					uni.hideLoading();
					uni.stopPullDownRefresh();
				})
			},

			initComponentData() {
				if (this.deviceVo.status != 1) {
					getApp().err("设备状态异常");
					return;
				}
				let setT;
				try {
					// #ifndef  MP-TOUTIAO
					setT = setTimeout(() => { // 该定时器防止子组件未渲染导致获取不到
						if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.CRANE_MACHINE || this.deviceVo
							.deviceTypeId == this.deviceTypeEnum.CARD_MACHINE || this.deviceVo
							.deviceTypeId == this.deviceTypeEnum.MULTI_SPLIT) {
							this.$refs['ufoCatcherV3'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.GASHAPON_MACHINE) {
							this.$refs['tweakEgg'].init(this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum
							.CONVERTIBLE_CURRENCY_MACHINE || this.deviceVo.deviceTypeId == this.deviceTypeEnum
							.SUPER_MONEY_CHANGER) {
							this.$refs['coinChangerV2'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.WA_TANG_MACHINE) {
							this.$refs['digSugar'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.SHOUT_MACHINE) {
							this.$refs['shoutingFountain'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.RAILS_MACHINE) {
							this.$refs['monorailPulley'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.BATTERY_CAR_CHARGING_PILE) {
							this.$refs['batteryCarChargingPile'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.PERAMBULATOR) {
							this.$refs['buggy'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.SHARED_ARCADE) {
							this.$refs['sharedArcade'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.CURRENCY_TYPE || this.deviceVo
							.deviceTypeId == this.deviceTypeEnum.SINGING_MACHINE) {
							this.$refs['currencyType'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.VENDING_MACHINE ||
								this.deviceVo.deviceTypeId == this.deviceTypeEnum.OPEN_DOOR_VENDING_MACHINE || this.deviceVo.deviceTypeId == this.deviceTypeEnum.BLEND_VENDING_MACHINE) {
							this.$refs['vendingMachine'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.LOTTERY_MACHINE) {
							this.$refs['lotteryMachine'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.CASH_REGISTER || this.deviceVo
							.deviceTypeId == this.deviceTypeEnum.TICKET_DEPOSITING_MACHINE) {
							this.$refs['depositNotes'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.COUCH_FOR_MASSAGE) {
							this.$refs['couchForMassage'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.PUDDLE_JUMPER) {
							this.$refs['puddleJumper'].init(this.deviceNo, this.deviceVo);
						}
						clearTimeout(setT);
					}, 200)
					// #endif

					// #ifdef  MP-TOUTIAO
					this.$nextTick(() => {
						if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.CRANE_MACHINE || this.deviceVo
							.deviceTypeId == this.deviceTypeEnum.CARD_MACHINE || this.deviceVo
							.deviceTypeId == this.deviceTypeEnum.MULTI_SPLIT) {
							this.$refs['ufoCatcherV3'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.GASHAPON_MACHINE) {
							this.$refs['tweakEgg'].init(this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum
							.CONVERTIBLE_CURRENCY_MACHINE || this.deviceVo.deviceTypeId == this.deviceTypeEnum
							.SUPER_MONEY_CHANGER) {
							this.$refs['coinChangerV2'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.WA_TANG_MACHINE) {
							this.$refs['digSugar'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.SHOUT_MACHINE) {
							this.$refs['shoutingFountain'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.RAILS_MACHINE) {
							this.$refs['monorailPulley'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.BATTERY_CAR_CHARGING_PILE) {
							this.$refs['batteryCarChargingPile'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.SHARED_ARCADE) {
							this.$refs['sharedArcade'].init(this.deviceNo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.CURRENCY_TYPE || this.deviceVo
							.deviceTypeId == this.deviceTypeEnum.SINGING_MACHINE) {
							this.$refs['currencyType'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.VENDING_MACHINE ||
								this.deviceVo.deviceTypeId == this.deviceTypeEnum.OPEN_DOOR_VENDING_MACHINE || this.deviceVo.deviceTypeId == this.deviceTypeEnum.BLEND_VENDING_MACHINE) {
							this.$refs['vendingMachine'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.LOTTERY_MACHINE) {
							this.$refs['lotteryMachine'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.CASH_REGISTER || this.deviceVo
							.deviceTypeId == this.deviceTypeEnum.TICKET_DEPOSITING_MACHINE) {
							this.$refs['depositNotes'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.COUCH_FOR_MASSAGE) {
							this.$refs['couchForMassage'].init(this.deviceNo, this.deviceVo);
						} else if (this.deviceVo.deviceTypeId == this.deviceTypeEnum.PUDDLE_JUMPER) {
							this.$refs['puddleJumper'].init(this.deviceNo, this.deviceVo);
						}
					});
					// #endif
				} catch (e) {
					clearTimeout(setT);
					console.error(e.toString())
				}
			},

			navMemberCenter() {
				uni.switchTab({
					url: '/pages/member/member-center/member-center'
				});
			},
		}
	}
</script>

<style>

</style>
